local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();
local Tool=_G.CommonOHOH.load.WeaponTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.StartAttackEvent=self.parent.StartAttackEvent
    self.character=self.parent.character
    self.equipId=self.parent:GetEquipId()

    self.Humanoid=self.character:GetHumanoid()
    self.Animatior= self.character:AddComponent(CommonOHOH.load.CharacterAnimationCtrl());

    local artInfo= Tool.GetWeaponArt(self.equipId)

    --加载拿武器动画
    self.EquipAnimationId=artInfo.Animation.equip:GetAttribute("Id")
    local a=artInfo.Animation.equip:Clone()
    a.Name= self.EquipAnimationId

    local track=self.Animatior:LoadAnimation(a,self.EquipAnimationId)
    track.Looped=true

    --加载武器攻击动画
    self.AttackAnimationId=artInfo.Animation.attack:GetAttribute("Id")
    local a2=artInfo.Animation.attack:Clone()
    a2.Name= self.AttackAnimationId
    self.Animatior:LoadAnimation(a2,self.AttackAnimationId)

    --开始播放拿武器动画
    self.WaitEquipCon=self:AddProcess(function()
        self:Wait(0.5)
        self.Animatior:PlayAnimation(self.EquipAnimationId)
    end)

    --监听武器攻击事件
    CallBackManager:AddRbxConnect(self, self.StartAttackEvent, self.StartAttack);
end;


--播放攻击动画
function M.StartAttack(self)

    if  self.WaitEquipCon then
        self.WaitEquipCon:Disconnect()
        self.WaitEquipCon=nil
    end

  self.Animatior:StopAnimation(self.EquipAnimationId)
  local dt=  self.Animatior:PlayAnimation(self.AttackAnimationId,"Attack")
  dt=dt or 0
  self.WaitEquipCon= self:AddProcess(function()
        self:Wait(dt)
        self.Animatior:PlayAnimation(self.EquipAnimationId)
   end)
end;


function M.dtor(self)

    if  self.WaitEquipCon then
        self.WaitEquipCon:Disconnect()
        self.WaitEquipCon=nil
    end

	M.super.dtor(self);
end;


return M